<?php


/**
 * Notes:
 * PROJECT_NAME : ERP v1.8 重构erp
 * User: 夏鱼儿<397072174@qq.com>
 * Date: 2024/6/22
 * Time: 16:45
 */

namespace app\tms\controller;

use app\company\model\Warehouse;
use app\mini\model\Store;
use app\tms\model\DeliverDriver;
use app\tms\model\DeliverTask;
use Exception;
use think\db\exception\DbException;
use think\Response;

class My extends Base
{

    /**
     * 钱包
     * @return Response
     */
    public function amount(): Response
    {
        $money_all      = $this->userInfo->deliverDriverAccountLogs()->where('type', 1)->sum('amount');//总金额
        $money_withdraw = $this->userInfo->deliverDriverAccountLogs()->where('type', 2)->sum('amount');//提现金额
        $money          = $this->userInfo->money;
        return $this->success('请求成功', compact('money', 'money_all', 'money_withdraw'));
    }


    /**
     * 提现
     * @param $amount
     * @return Response
     */
    public function withdraw($amount): Response
    {
        if ($amount <= 0) return $this->error('提现金额必须大于0');
        $model = new DeliverDriver();
        $model->startTrans();
        try {
            //扣减余额
            $money = $this->userInfo->money;
            if ($money < $amount) exception('余额不足');
            $this->userInfo->inc('money', $amount * -1)->update();
            //添加提现记录
            $no = 'TX' . date("Ymd") . rand(10000, 99999);
            $model->deliverDriverWithdraws()->save([
                'deliver_driver_id' => $this->user_id,
                'company_id'        => $this->company_id,
                'no'                => $no,
                'amount'            => $amount,
                'remark'            => request()->param('remark', '')
            ]);

            //添加提现日志
            $model->deliverDriverAccountLogs()->save([
                    'deliver_driver_id' => $this->user_id,
                    'company_id'        => $this->company_id,
                    'withdraw_no'       => $no,
                    'type'              => 2,
                    'amount'            => $amount,
                    'money'             => $money]
            );
            $model->commit();
            return $this->success('提现成功');
        } catch (Exception $e) {
            $model->rollback();
            return $this->error($e->getMessage());
        }
    }


    /**
     * 提现记录
     * @param int $limit
     * @return Response
     */
    public function withdrawList(int $limit = 10): Response
    {
        $list = $this->userInfo->deliverDriverWithdraws()->field('id,amount,status,create_time,type')
                               ->order('id desc')->paginate($limit)->append(['driver_withdraw_status_text']);
        return $this->success('', compact('list'));
    }

    /**
     * 任务总数
     * @return Response
     * @throws DbException
     */
    public function taskCount(): Response
    {
        //任务总数
        $taskCount = DeliverTask::user($this->user_id)->count();
        //未完成任务数量
        $newTaskCount = DeliverTask::user($this->user_id)->taskStatus(1)->count();
        //进行中的任务数量
        $doingTaskCount = DeliverTask::user($this->user_id)->taskStatus(2)->count();
        //已完成任务数量
        $doneTaskCount = DeliverTask::user($this->user_id)->taskStatus(3)->count();
        //网点数量
//        $id         = $this->userInfo->deliverLineO()->value('id');
//        $storeCount = Store::where('deliver_line_id', $id)->count();
        //todo 司机可以绑定多个路线/还是司机只能绑定一个路线/或者司机不绑定路线,只绑定任务
        $id         = $this->userInfo->deliverLines()->column('id');
        $storeCount = Store::whereIn('deliver_line_id', $id)->count();
        //仓库数量
        $warehouseCount = Warehouse::gs($this->company_id)->count();
        return $this->success('请求成功', compact('newTaskCount', 'doingTaskCount', 'doneTaskCount', 'taskCount', 'storeCount', 'warehouseCount'));
    }


    /**
     * 路线网点列表
     * @param int $limit
     * @return Response
     * @throws DbException
     */
    public function linStoreList(int $limit = 10): Response
    {
//        $id   = $this->userInfo->deliverLineO()->value('id');
//        $list = Store::where('deliver_line_id', $id)->field('id,name,address')->paginate($limit)->each(function ($item) {
//            $item->real_name = $item->storeUsers()->value('real_name');
//            $item->mobile    = $item->storeUsers()->value('mobile');
//        });
        //todo 司机可以绑定多个路线/还是司机只能绑定一个路线/或者司机不绑定路线,只绑定任务
        $id   = $this->userInfo->deliverLines()->column('id');
        $list = Store::whereIn('deliver_line_id', $id)->field('id,name,address')->paginate($limit)->each(function ($item) {
            $item->real_name = $item->storeUsers()->value('real_name');
            $item->mobile    = $item->storeUsers()->value('mobile');
        });
        return $this->success('', compact('list'));
    }


    /**
     * 仓库列表
     * @param int $limit
     * @return Response
     * @throws DbException
     */
    public function warehouseList(int $limit = 10): Response
    {
        $list = Warehouse::gs($this->company_id)->field('id,name,address,real_name,mobile')->paginate($limit);
        return $this->success('', compact('list'));
    }

}